package com.lovo.test.ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//改密界面
public class CorrectPanel extends JPanel implements ActionListener {
    private ATM frame;

    //以下是按钮
    private JButton confirmBut;//确认按钮
    private JButton returnBut;//返回按钮
    private JLabel newLabels;//新密码标签
    private JLabel lodLabels;//旧密码标签
    private JPasswordField oldPwd;//旧密码输入框
    private JPasswordField newPwd;//新密码输入框
    public CorrectPanel(ATM frame){
        this.frame=frame;

        //设置空布局
        this.setLayout(null);
        //设置透明
        this.setOpaque(false);


        //以下是密码框
        oldPwd = new JPasswordField();//旧密码框
        oldPwd.setBounds(350,200,100,20);
        this.add(oldPwd);

        newPwd = new JPasswordField();//新密码框
        newPwd.setBounds(350,250,100,20);
        this.add(newPwd);

        //以下是标签
        //旧密码标签
        lodLabels = new JLabel("请输入旧密码:");
        lodLabels.setBounds(250,200,100,20);
        add(lodLabels);
        //新密码标签
        newLabels = new JLabel("请输入旧密码:");
        newLabels.setBounds(250,250,100,20);
        add(newLabels);


        //以下是按钮
        //确认按钮,确认存款金额
        confirmBut = new JButton("确认");
        confirmBut.setBounds(450,280,100,50);
        this.add(confirmBut);
        confirmBut.addActionListener(this);

        //返回按钮,返回菜单界面
        returnBut = new JButton("返回");
        returnBut.setBounds(350,280,100,50);
        this.add(returnBut);
        returnBut.addActionListener(this);

    }

    //改密函数
    public void modify(){
        String oldPwdS=String.valueOf(oldPwd.getPassword());//获取用户输入的旧密码并转换成字符串
        String newPwdS =String.valueOf(newPwd.getPassword()) ;//获取用户输入的新密码并转换成字符串
        if (oldPwdS.equals(frame.getUserName().getPassWord())){
            frame.getUserName().setPassWord(newPwdS);
            JOptionPane.showMessageDialog(this,"修改密码成功，返回登录界面");
            frame.switchPanel("登录");
        }else {
            JOptionPane.showMessageDialog(this,"旧密码错误");
        }
    }


  //事件监听器
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==confirmBut){//确认改密事件
            modify();
            frame.preserve();

        }else if (e.getSource()==returnBut){//返回菜单事件
            frame.switchPanel("菜单");

        }

    }
}
